﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using VSSTQH.Properties;
using System.IO.Ports;
using System.Drawing.Printing;

namespace VSSTQH
{
    class Common
    {
        internal static UserModel UserLogin;
        internal static SerialPort Port;
        internal static SerialPort Port2;

        internal static string Template = @"Template\Ticket.dot";
        internal static string AdminAccount = "admin";
        internal static string defaultUsername = "nimda";
        internal static string defaultPassword = "091";
        internal static string logfile = "log.txt";
        internal static string RememberPath = Path.GetTempPath() + "remeberfile.vss";
        internal static string MM_S2C
        {
            get { return Settings.Default.MM_S2C + Settings.Default.FileMM_S2C; }
        }
        internal static string SD_S2C
        {
            get { return Settings.Default.SD_S2C + Settings.Default.FileSD_S2C; }
        }
        internal static string MM_C2S
        {
            get { return Settings.Default.MM_C2S + Settings.Default.FileMM_C2S; }
        }
        internal static string SD_C2S
        {
            get { return Settings.Default.SD_C2S + Settings.Default.FileSD_C2S; }
        }
        internal static string Stored
        {
            get { return Settings.Default.Stored; }
        }

        #region MessageBox
        internal static bool? ShowQuestion(string message, params object[] param)
        {
            DialogResult result = MessageBox.Show(String.Format(message, param), "Thông báo", MessageBoxButtons.YesNoCancel,
                MessageBoxIcon.Question, MessageBoxDefaultButton.Button3);

            if (result == DialogResult.Yes) { return true; }
            else if (result == DialogResult.No) { return false; }

            return null;
        }

        internal static bool ShowYesNoQuestion(string message, params object[] param)
        {
            DialogResult result = MessageBox.Show(String.Format(message, param), "Thông báo", MessageBoxButtons.YesNo,
                MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);

            return result == DialogResult.Yes;
        }

        internal static bool ShowOKCancelQuestion(string message, params object[] param)
        {
            DialogResult result = MessageBox.Show(String.Format(message, param), "Thông báo", MessageBoxButtons.OKCancel,
                MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);

            return result == DialogResult.Yes;
        }

        internal static void ShowAlert(string message, params object[] param)
        {
            DialogResult result = MessageBox.Show(String.Format(message, param), "Thông báo", MessageBoxButtons.OK,
                MessageBoxIcon.Error);
        }

        internal static void ShowSuccessful(string message, params object[] param)
        {
            DialogResult result = MessageBox.Show(String.Format(message, param), "Thông báo", MessageBoxButtons.OK,
                MessageBoxIcon.Information);
        } 
        #endregion

        internal static void InitPort()
        {
            try
            {
                ParaPort paraPort = new ParaPort();
                if (Port == null)
                    Port = new System.IO.Ports.SerialPort();
                
                if (!Port.IsOpen)
                {
                    // Config ports.
                    Port.PortName = paraPort.PortName;
                    Port.BaudRate = paraPort.BaudRate;
                    Port.Parity = (System.IO.Ports.Parity)paraPort.Parity;
                    Port.DataBits = paraPort.DataBits;
                    Port.StopBits = (System.IO.Ports.StopBits)paraPort.StopBits;
                }
            }
            catch (Exception ex)
            {
                ShowAlert(Resources.NotConnectPort + ex.Message);
            }
        }
        internal static void InitPort2()
        {
            try
            {
                ParaPort paraPort = new ParaPort();
                if (Port2 == null)
                    Port2 = new System.IO.Ports.SerialPort();

                if (!Port2.IsOpen)
                {
                    // Config ports.
                    Port2.PortName = paraPort.PortName2;
                    Port2.BaudRate = paraPort.BaudRate2;
                    Port2.Parity = (System.IO.Ports.Parity)paraPort.Parity2;
                    Port2.DataBits = paraPort.DataBits2;
                    Port2.StopBits = (System.IO.Ports.StopBits)paraPort.StopBits2;
                }
            }
            catch (Exception ex)
            {
                ShowAlert(Resources.NotConnectPort + ex.Message);
            }
        }
        public static bool CheckPrinter(string printerName)
        {
            foreach (var item in PrinterSettings.InstalledPrinters)
            {
                if (item.Equals(printerName))
                    return true;
            }
            return false;
        }
    }
}
